//
//  DZMRMTopView.swift
//  DZMeBookRead
//
//  Created by 邓泽淼 on 2017/5/11.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit

class DZMRMTopView: DZMRMBaseView {
    
    /// 返回按钮
    private lazy var buttonBack = UIButton(type: .custom)
    // tobTitle
    private lazy var labelTitle = UILabel()
    /// 书签
    private lazy var buttonMore = UIButton(type: .custom)
    
    override func addSubviews() {
        super.addSubviews()
        
        let barView = UIView()
        barView.backgroundColor = UIColor.clear
        self.addSubview(barView)
        barView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(44.0)
        }
        
        // 返回
        self.buttonBack.setImage(UIImage(named: "button_back", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
        self.buttonBack.addTarget(self, action: #selector(self.goBack(button:)), for: .touchUpInside)
        barView.addSubview(self.buttonBack)
        self.buttonBack.snp.makeConstraints { (make) in
            make.width.height.equalTo(44.0)
            make.left.bottom.equalToSuperview()
        }
        
        // more
        self.buttonMore.setImage(UIImage(named: "button_more", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
        self.buttonMore.addTarget(self, action: #selector(self.clickMore(button:)), for: .touchUpInside)
        barView.addSubview(self.buttonMore)
        self.buttonMore.snp.makeConstraints { (make) in
            make.width.height.equalTo(44.0)
            make.right.bottom.equalToSuperview()
        }
        
        // leftTitle
        self.labelTitle.textColor = UIColor.white
        self.labelTitle.textAlignment = .center
        barView.addSubview(self.labelTitle)
        self.labelTitle.snp.makeConstraints { (make) in
            make.left.equalTo(self.buttonBack.snp.right).offset(5.0)
            make.bottom.equalToSuperview()
            make.height.equalTo(44.0)
            make.right.equalTo(self.buttonMore.snp.left).offset(-5.0)
        }

    }
    
    @objc private func clickMore(button:UIButton) {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuClickMoreButton?(readMenu: readMenu_, button: button)
    }
    
    
    /**
     设置头部标题
     - parameter title: 标题
     */
    func setTitle(_ title:String?) {
        self.labelTitle.text = title
    }
    
    @objc private func goBack(button:UIButton) {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuClickBackButton?(readMenu: readMenu_, button: button)
    }
    
}
